package com.szakul.pdf.validators;

import com.vaadin.data.validator.AbstractValidator;

public class PagesValidator extends AbstractValidator {
	   
    private static final long serialVersionUID = -9203475720179860875L;
    private int range = 0;

    public PagesValidator(String errorMessage) {
        super(errorMessage);
    }

	@Override
	public void setErrorMessage(String errorMessage) {
		super.setErrorMessage(errorMessage);
	}
	
    public boolean isValid(Object value) {
        if (value == null) {
            return false;
        }
        if(value instanceof Integer){
        	
        	int valueInt = (Integer)value;
        	
        	if(valueInt >0 && valueInt <= range) {
        		return true;
        	}
     
        	return false;
 
        }
       
        if(value instanceof String){
            String strVal = (String)value;
            try {
            	int valueInt = Integer.parseInt(strVal);
            	if(valueInt >0 && valueInt <= range) {
            		return true;
            	}
                return false;
            } catch (Exception ignoredException) {
                return false;
            }
            
        }
       
        return false;
    }


	public int getRange() {
		return range;
	}


	public void setRange(int range) {
		this.range = range;
	}
    
    
}